/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OfficeOpenXMLInterop;

namespace Theme_Builder
{
    public partial class EditOuterShadow : Form
    {
        public EditOuterShadow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets/Sets the CT_OuterShadowEffect that this form is responsible for editing
        /// </summary>
        public OfficeOpenXMLInterop.CT_OuterShadowEffect OuterShadow
        {
            get
            {
                CT_OuterShadowEffect shadow = new CT_OuterShadowEffect();

                shadow.srgbClr = new CT_SRgbColor();
                shadow.srgbClr.val = Converters.ColorToByteRBG(this.panelColor.BackColor);

                int fooInt = Int32.Parse(this.textBoxAlpha.Text);
                if (fooInt != 100)
                {
                    shadow.srgbClr.alpha = new CT_PositiveFixedPercentage();
                    shadow.srgbClr.alpha.val = fooInt * 1000;
                }

                if (this.comboBoxAlignment.SelectedIndex >= 0)
                {
                    shadow.algn = this.ConvertAlignment(this.comboBoxAlignment.SelectedItem.ToString());
                }

                shadow.rotWithShape = this.checkBoxRotateWithShape.Checked;

                shadow.blurRad = (long)(float.Parse(this.textBoxBlurRad.Text) * 12700f);
                shadow.dist = (long)(float.Parse(this.textBoxDist.Text) * 12700f);
                shadow.dir = (int)(float.Parse(this.textBoxDir.Text) * 60000f);

                fooInt = (int)(float.Parse(this.textBoxSkewX.Text) * 60000f);
                if (fooInt != 0)
                {
                    shadow.kx = fooInt;
                }

                fooInt = (int)(float.Parse(this.textBoxSkewY.Text) * 60000f);
                if (fooInt != 0)
                {
                    shadow.ky = fooInt;
                }

                fooInt = (int)(float.Parse(this.textBoxScaleX.Text) * 1000f);
                if (fooInt != 100)
                {
                    shadow.sx = fooInt;
                }

                fooInt = (int)(float.Parse(this.textBoxScaleY.Text) * 1000f);
                if (fooInt != 100)
                {
                    shadow.sy = fooInt;
                }

                return shadow;
            }
            set
            {
                if (value.srgbClr != null)
                {
                    this.panelColor.BackColor = Converters.ByteRGBToColor(value.srgbClr.val);

                    if (value.srgbClr.alpha != null)
                    {
                        this.textBoxAlpha.Text = ((float)(value.srgbClr.alpha.val) / 1000f).ToString();
                    }
                }

                this.comboBoxAlignment.SelectedIndex = this.ConvertAlignmentToPosition(value.algn);

                this.checkBoxRotateWithShape.Checked = value.rotWithShape;

                this.textBoxBlurRad.Text = ((float)(value.blurRad) / 12700f).ToString();
                this.textBoxDist.Text = ((float)(value.dist) / 12700f).ToString();
                this.textBoxDir.Text = ((float)(value.dir) / 60000f).ToString();

                if (value.kx != 0)
                {
                    this.textBoxSkewX.Text = ((float)(value.kx) / 60000f).ToString();
                }

                if (value.ky != 0)
                {
                    this.textBoxSkewY.Text = ((float)(value.ky) / 60000f).ToString();
                }

                if (value.sx != 0 && value.sx != 100000)
                {
                    this.textBoxScaleX.Text = ((float)(value.sx) / 1000f).ToString();
                }

                if (value.sy != 0 && value.sy != 100000)
                {
                    this.textBoxScaleY.Text = ((float)(value.sy) / 1000f).ToString();
                }
            }
        }

        // TODO: more stuff to move into the converter area of utility
        private OfficeOpenXMLInterop.ST_RectAlignment ConvertAlignment(string alignment)
        {
            switch (alignment)
            {
                case "Bottom":
                    return ST_RectAlignment.b;
                case "Bottom Left":
                    return ST_RectAlignment.bl;
                case "Bottom Right":
                    return ST_RectAlignment.br;
                case "Center":
                    return ST_RectAlignment.ctr;
                case "Left":
                    return ST_RectAlignment.l;
                case "Right":
                    return ST_RectAlignment.r;
                case "Top":
                    return ST_RectAlignment.t;
                case "Top Left":
                    return ST_RectAlignment.tl;
                case "Top Right":
                    return ST_RectAlignment.tr;
                default:
                    throw (new Exception("Unknown alignment - EditOuterShadow.ConvertAlignment: " + alignment));
            }
        }

        // TODO: more stuff to move into the converter area of utility
        private int ConvertAlignmentToPosition(OfficeOpenXMLInterop.ST_RectAlignment alignment)
        {
            switch (alignment)
            {
                case ST_RectAlignment.b:
                    return 0;
                case ST_RectAlignment.bl:
                    return 1;
                case ST_RectAlignment.br:
                    return 2;
                case ST_RectAlignment.ctr:
                    return 3;
                case ST_RectAlignment.l:
                    return 4;
                case ST_RectAlignment.r:
                    return 5;
                case ST_RectAlignment.t:
                    return 6;
                case ST_RectAlignment.tl:
                    return 7;
                case ST_RectAlignment.tr:
                    return 8;
                default:
                    return -1;
            }
        }

        private void panelColor_MouseClick(object sender, MouseEventArgs e)
        {
            this.colorDialog1.Color = this.panelColor.BackColor;

            if (this.colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                this.panelColor.BackColor = this.colorDialog1.Color;

                // Invalidate the panel so that it repaints
                this.panelColor.Invalidate();
            }
        }

        private void panelColor_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromHwnd(this.panelColor.Handle);

            RectangleF bounds = g.VisibleClipBounds;

            ColorMods cMods = new ColorMods();
            cMods.alpha = Double.Parse(this.textBoxAlpha.Text);

            Color color = Converters.ModifyColor(this.panelColor.BackColor, cMods);

            SolidBrush sb = new SolidBrush(color);

            g.FillRectangle(sb, bounds);

            g.Dispose();
        }

        private void textBoxAlpha_Validated(object sender, EventArgs e)
        {
            this.panelColor.Invalidate();
        }
    }
}